<?php
namespace app\system\controller;

use app\common\model\ShopOrderModel;
use app\common\model\SiteModel;
use app\common\model\UserModel;
use \think\Cache;
use think\Request;

class ShopOrderInvoiceController extends BaseController
{
    private $orderModel;
    private $userModel;
    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->orderModel = new ShopOrderModel();
        $this->userModel = new UserModel();
    }

    /**
     * 订单发票
     * @return mixed
     */
    public function index(){
        $data = $this->request->param();
        if(!empty($data['order_sn'])){
            $map['order_sn'] = ['like',"%".$data['order_sn']."%"];
            $query['order_sn'] = $data['order_sn'];
        }
        $map['order_status'] = 2;
        if(empty($data['invoice'])){
            $data['invoice'] = 0;
        }
        if($data['invoice']>0){
            $map['invoice'] = $data['invoice'];
        }else{
            $map['invoice'] = ['in',[1,2,3]];
        }
        $query['invoice'] = $data['invoice'];
        $orderList = $this->orderModel->where($map)->paginate(12,false,['query'=>$query,]);
        $area = cache('area');
        foreach($orderList as $k=>$v){
            $areaName = $area[$v['invoice_info']['inv_info']['addr_prov_id']].' '.$area[$v['invoice_info']['inv_info']['addr_city_id']].' '.$area[$v['invoice_info']['inv_info']['addr_dist_id']];
            $orderList[$k]['invoice_info_area_name'] = $areaName;
        }
        $this->assign('orderList',$orderList);
        $this->assign('query',$query);
        return $this->fetch();
    }

    public function edit(){
        if($this->request->isPost()){
            $orderId = $this->request->param('order_id');
            $orderInfo = $this->orderModel->where('order_id',$orderId)->find();
            $orderOldInvoice = $orderInfo['invoice_info']; //旧开票信息
            $invoiceInfo = $this->request->param('invoice_info/a'); //新开票信息
            $invoiceInfo = array_merge($orderOldInvoice,$invoiceInfo);
            $invoice = $this->request->param('invoice');
            $invoiceSn = $this->request->param('invoice_sn');
            if($invoice==2 && empty($invoiceSn)){
                $this->error('请填写开票号码！');
            }
            $data = [
                'order_id'=>$orderId,
                'invoice'=>$invoice,
                'invoice_sn'=>$invoiceSn,
                'invoice_info'=>json_encode($invoiceInfo)
            ];
            $this->orderModel->editData($data);
            $this->success('保存成功！',url('index'));
        }
        $orderId = $this->request->param('order_id');
        $info = $this->orderModel->where('order_id',$orderId)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function del(){
        $orderId = $this->request->param('order_id');
        $data = [
            'order_id'=>$orderId,
            'invoice'=>0,
            'invoice_info'=>'',
        ];
        $this->orderModel->editData($data);
        $this->success('操作成功！');
    }

}
